﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _3DTools;
using System.Windows.Media.Media3D;
using System.Windows.Media;

namespace WpfBrowserApplication1
{
    class Interactive3D:  InteractiveVisual3D
    {

        public Interactive3D()
        {
            Geometry = getmesh();
        }
        //每个母线10个点，一周36条母线共360个点
        Vector3D[] vertices = new Vector3D[360];
        void initVertices()
        {


            for (int i = 0; i < 10; i++)
            {
                vertices[i] = new Vector3D(i, i, 0);
            }
            int angle = 0;

            Matrix3D matrixRotate = new Matrix3D();
            for (int i = 1; i < 36; i++)
            {
                angle = i * 10;
                matrixRotate.M11 = 1;
                double myradian = Math.PI * angle / 180;
                matrixRotate = new Matrix3D(
                  1, 0, 0, 0,
                  0, Math.Cos(myradian), Math.Sin(myradian), 0,
                  0, -Math.Sin(myradian), Math.Cos(myradian), 0,
                  0, 0, 0, 1

                  );



                for (int j = 0; j < 10; j++)
                {
                    vertices[angle + j] = vertices[j] * matrixRotate;
                }

            }
        }

        internal MeshGeometry3D getmesh()
        {
          
            initVertices();
            MeshGeometry3D triangleMesh = new MeshGeometry3D();
          

            Point3D point10 = new Point3D(0, 0, 0);
            Point3D point11 = new Point3D(1, 0, 0);
            Point3D point12 = new Point3D(0, 1, 0);
            for (int i = 0; i < 360; i++)
            {
                triangleMesh.Positions.Add(new Point3D(vertices[i].X, vertices[i].Y, vertices[i].Z));
            }

            int j = 0;
            for (int i = 0; i < 35; i++)
            {
                //if(i%3==0)glColor3f(1.0f,0.0f,0.0f);
                //if(i%3==1)glColor3f(0.0f,1.0f,0.0f);
                //if(i%3==2)glColor3f(0.0f,0.0f,1.0f);
                j = i * 10;
                for (int x = j; x < j + 9; x++)
                {
                    triangleMesh.TriangleIndices.Add(x);
                    triangleMesh.TriangleIndices.Add(x + 1);
                    triangleMesh.TriangleIndices.Add(x + 10);

                    triangleMesh.TriangleIndices.Add(x + 10);
                    triangleMesh.TriangleIndices.Add(x + 1);
                    triangleMesh.TriangleIndices.Add(x + 11);
                }
            }

            for (int x = 350; x < 360; x++)
            {
                triangleMesh.TriangleIndices.Add(x);
                triangleMesh.TriangleIndices.Add(x + 1);
                triangleMesh.TriangleIndices.Add(x - 350);

                triangleMesh.TriangleIndices.Add(x - 350);
                triangleMesh.TriangleIndices.Add(x + 1);
                triangleMesh.TriangleIndices.Add(x - 349);


            }

            //Vector3D normal = new Vector3D(0, 0, 1);
            //triangleMesh.Normals.Add(normal);
            //triangleMesh.Normals.Add(normal);
            //triangleMesh.Normals.Add(normal);
            triangleMesh.Freeze();
            return triangleMesh;
          


            //Storyboard storyboard3 = new Storyboard();
            //DoubleAnimation animadouble = new DoubleAnimation();
            //animadouble.From = 0;
            //animadouble.To = 360;
            //animadouble.Duration = new Duration(new TimeSpan(0, 0, 2));
            //Storyboard.SetTargetName(animadouble, "axisRotation3D");
            //Storyboard.SetTargetProperty(animadouble, new PropertyPath(AxisAngleRotation3D.AngleProperty));
            //storyboard3.Children.Add(animadouble);

            //storyboard3.RepeatBehavior = RepeatBehavior.Forever;
            //storyboard3.Begin(this);
        }
    }
}
